<template>
  <view class="my-settle-container">
    <!-- 全选 -->
      <label class="radio" @click="changeAllState">
        <radio color="#c00000"  :checked="ifFullCheck" /><text>全选</text>
      </label>
      <!-- 合计 -->
      <view class="amount-box">
        合计:<text class="amount">￥{{checkGoodsMaount}}</text>
        
      </view>
      <view class="rm-box" @click="rmgoods">
        删除
      </view>
      <!-- 结算 -->
      <view class="btn-settle" @click="settlement">结算({{checkedCound}})</view>
  </view>
</template>

<script>
  
  import {mapState,mapGetters,mapMutations} from 'vuex'
  export default {
    name:"my-settle",
    data() {
      return {
        seconds:3,
        timer:null
      };
    },
    methods: {
      ...mapMutations('m_cart',['updateAllGoodsState','romvgoods']),
      ...mapMutations('m_user',['updateRedirectInfo']),
      changeAllState(){
        this.updateAllGoodsState(!this.ifFullCheck)
      },
      // 删除
      rmgoods(){
        if(!this.checkedCound) return uni.$showMsg('请选择商品！')
        uni.showModal({
            title: '提示',
            content: '确定删除商品',
            success: (res) =>{
              if(res.confirm)
              this.romvgoods()
              else uni.$showMsg("删除商品失败")
            }
        })
      },
      // 结算
      settlement(){
        if(!this.checkedCound) return uni.$showMsg('请选择商品！')
        if(!this.token){
          uni.$showMsg('请先登录')
          this.delayNavigate()
        }
        else if(!this.addstr) 
        return uni.$showMsg('请选择收获地址！')
        
        // 支付
        this.payOrder()
      },
      delayNavigate(){
        this.seconds = 3
        this.showTips(this.seconds)
        this.timer = setInterval(()=>{
          this.seconds --
          if(this.seconds <= 0){
            clearInterval(this.timer)
            uni.switchTab({
              url:'/pages/my/my',
              success:()=>{
                this.updateRedirectInfo({
                  openType:'switchTab',
                  from:'/pages/cart/cart'
                })
              }
            })
            return 
          }
          this.showTips(this.seconds)
        },1000)
      },
      showTips(num){
        uni.showToast({
          icon:'none',
          title:'请先登录后再结算' + num + '秒后自动跳转到登录页',
          mask:true,
          duration:1500
        })
      },
      // 支付
      async payOrder(){
        // 创建订单
        const orderInfo = {
          // order_price: this.checkGoodsMaount,
          order_price: 0.01,
          consignee_addr: this.addstr,
          goods: this.cart.filter(x => x.goods_state).map(x => ({
            goods_id: x.goods_id,
            goods_number:x.goods_count,
            goods_price:x.goods_price
          }))
        }
        const { data: res } = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
        if(res.meta.state !== 200) return uni.$showMsg('订单创建失败！')
        // 订单编号
        const orderNumber = res.message.order_number
        
        // 预支付
        const {data: res2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{
          order_number: orderNumber
        })
        if(res2.meta.state !== 200) return uni.$showError('订单生成出错！')
        const payInfo = res2.message.pay
        
        // 发起支付
        const [err,succ] = await uni.requestPayment(payInfo)
        if(err) return uni.$showMsg('订单未支付')
        
        const {data:res3} = await uni.$http.post('/api/public/v1/my/orders/chkOrder',{
          order_number: orderNumber
        })
        if(res3.meta.state !== 200) return uni.$showError('订单未支付')
        uni.showToast({
          title:'支付成功！',
          icon:'success'
        })
      }
    },
    computed:{
      ...mapGetters('m_cart',['checkedCound','total','checkGoodsMaount']),
      ...mapGetters('m_user',['addstr']),
      ...mapState('m_user',['token']),
      ...mapState('m_cart',['cart']),
      ifFullCheck(){
        return this.total === this.checkedCound
      }
    }
  }
</script>

<style lang="scss">
.my-settle-container{
  position: fixed;
  bottom: 0px;
  height: 35px;
  background-color: white;
  width: 100%;
  z-index: 100;
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  box-sizing: border-box;
  align-items: center;
  font-size: 14px;
  .radis {
    display: flex;
    align-items: center;
  }
  .amount{
    color: #c00000;
    
  }
  .rm-box{
    width: 80px;
    text-align: right;
    color: gray;
  }
  .btn-settle{
    height: 35px;
    min-width: 100px;
    background-color: #c00000;
    color: white;
    line-height: 35px;
    text-align: center;
  }
  
  
}
</style>